<?php

session_start();

if(empty($_SESSION["log"])){
	header('Location: login.php');
}

include_once("connexion.php");

if (isset($_GET['id']))
{
	$annonce_id = $_GET['id'];
	$sql = "SELECT * FROM bien WHERE id='".$annonce_id."';";
	$req = mysqli_query($db,$sql) or die('Erreur SQL !'.$sql.'<br>'.mysqli_error($db));

	$sql_id = "SELECT * FROM bien WHERE id='".$_GET['id']."';";
	$req_id = mysqli_query($db,$sql_id) or die('Erreur SQL !'.$sql_id.'<br>'.mysqli_error($db));
	
}
$sql_pays = "SELECT * from pays";
$req_pays = mysqli_query($db,$sql_pays) or die('Erreur SQL !'.$sql_pays.'<br>'.mysqli_error($db));

$sql_ville = "SELECT * from ville";
$req_ville = mysqli_query($db,$sql_ville) or die('Erreur SQL !'.$sql_ville.'<br>'.mysqli_error($db));

$sql_type = "SELECT * from type";
$req_type = mysqli_query($db,$sql_type) or die('Erreur SQL !'.$sql_type.'<br>'.mysqli_error($db));

if (isset($_POST['envoyer']))
{
		if (isset($_SESSION['id_user']))
			$id_user = $_SESSION['id_user'];

		$tarif_semaine = $_POST['tarif_semaine'];
		$tarif_journee = $_POST['tarif_journee'];
		$surface = $_POST['surface'];
		$error="";
		$description = $_POST['description'];
		$code_postal = $_POST['postal'];
		$adresse = $_POST['adresse'];
		$jour_debut = $_POST["jour_debut"];
		$mois_debut = $_POST["mois_debut"];
		$annee_debut = $_POST["annee_debut"];
		$nbsemaine = $_POST["nbsemaine"];

		$jour_fin = $jour_debut;
		$mois_fin = $mois_debut;
		$annee_fin = $annee_debut;

		$nbjour = $nbsemaine * 7;
		
		for ($i=0; $i < $nbjour ; $i++) { 
			if ($mois_fin%2 == 0) 
			{
				if ($jour_fin < 31 ) 
				{
					$jour_fin++;
				}
				else
				{
					$jour_fin = 1;
					$mois_fin++;
				}
			}
			else
			{
				if ($jour_fin < 30 ) 
				{
					$jour_fin++;
				}
				else
				{
					$jour_fin = 1;
					$mois_fin++;
				}
			}
			if ($mois_fin > 12) 
			{
				$mois_fin = 1;
				$annee_fin++;
			}
				
		}

		$debut_location = $annee_debut."-".$mois_debut."-".$jour_debut;
		$fin_location = $annee_fin."-".$mois_fin."-".$jour_fin;

		if (!empty($_POST['tarif_semaine'])) 
		{
			$query = "UPDATE tarif SET tarif_semaine='$tarif_semaine' WHERE id='".$_GET['id']."';";
			$res = mysqli_query($db, $query);
			if ($res == false)
				echo "Erreur dans la Tarification tarif_semain ";
			header("Refresh: 0;"); 
		}
		if (!empty($_POST['tarif_journee'])) 
		{
			$query = "UPDATE tarif SET tarif_jour='$tarif_journee' WHERE id='".$_GET['id']."';";
			$res = mysqli_query($db, $query);
			if ($res == false)
				echo "Erreur dans la Tarification tarif_journee ";
			header("Refresh: 0;"); 
		}

		$ville_bien_id1 = mysqli_fetch_array($req);
		$ville_bien_id = $ville_bien_id1['id_ville'];	
		$sql_ville_bien = "SELECT * from ville where id='$ville_bien_id'";
		$req_ville_bien = mysqli_query($db,$sql_ville_bien);
		$ville_bien1 = mysqli_fetch_array($req_ville_bien);
		$ville_bien = $ville_bien1['ville'];
		
		if ($_POST['ville']!=$ville_bien && !empty($_POST['postal'])) 
		{
				$ville = $_POST['ville'];
				$sql_ville = "SELECT * from ville WHERE ville ='$ville'";
				$req_ville = mysqli_query($db, $sql_ville);
				$res_ville = mysqli_num_rows($req_ville);

			if ($res_ville == 0) 
			{

				$query = "INSERT INTO ville VALUES ('', '".$ville."', '".$code_postal."');";
				$res = mysqli_query($db, $query);

				$sql_ville = "SELECT * from ville WHERE ville ='$ville'";
				$req_ville = mysqli_query($db, $sql_ville);
				$tab_ville = mysqli_fetch_array($req_ville);
				$id_ville = $tab_ville['id'];
				if ($res == false)
				echo "Erreur dans l'implementation de la ville";

				$query = "UPDATE bien SET id_ville='$id_ville' WHERE id='".$_GET['id']."';";
				$res = mysqli_query($db, $query);
				if ($res == false)
					echo "Erreur dans la Tarification id_ville ";
				header("Refresh: 0;"); 
			}
			else
			{
				$tab_ville = mysqli_fetch_array($req_ville);
				$id_ville = $tab_ville['id'];

				$query = "UPDATE bien SET id_ville='$id_ville' WHERE id='".$_GET['id']."';";
				$res = mysqli_query($db, $query);
				if ($res == false)
					echo "Erreur dans la Tarification id_ville ";
				header("Refresh: 0;"); 
			}
		}
		
		
		
		$pays_bien_id1 = mysqli_fetch_array($req_id);
		$pays_bien_id = $pays_bien_id1['id_pays'];	
		$sql_pays_bien = "SELECT * from pays where id='$pays_bien_id'";
		$req_pays_bien = mysqli_query($db,$sql_pays_bien);
		$pays_bien1 = mysqli_fetch_array($req_pays_bien);
		$pays_bien = $pays_bien1['pays'];
		if ($_POST['pays'] != $pays_bien) 
		{
				$pays = $_POST['pays'];
				$sql_pays = "SELECT * from pays WHERE pays ='$pays'";
				$req_pays = mysqli_query($db, $sql_pays);
				$res_pays = mysqli_num_rows($req_pays);

			if ($res_pays == 0) {

				$query = "INSERT INTO pays VALUES ('', '".$pays."');";
				$res = mysqli_query($db, $query);

				$sql_pays = "SELECT * from pays WHERE pays ='$pays'";
				$req_pays = mysqli_query($db, $sql_pays);
				$tab_pays = mysqli_fetch_array($req_pays);
				$id_pays = $tab_pays['id'];
				if ($res == false)
				echo "Erreur dans l'implementation de la pays";

				$query = "UPDATE bien SET id_pays='$id_pays' WHERE id='".$_GET['id']."';";
				$res = mysqli_query($db, $query);
				if ($res == false)
					echo "Erreur dans la Tarification id_pays "; 
				header("Refresh: 0;"); 
			}
			else
			{
				$tab_pays = mysqli_fetch_array($req_pays);
				$id_pays = $tab_pays['id'];
				
				$query = "UPDATE bien SET id_pays='$id_pays' WHERE id='".$_GET['id']."';";
				$res = mysqli_query($db, $query);
				if ($res == false)
					echo "Erreur dans la Tarification id_pays ";
				header("Refresh: 0;"); 
			}
		}

		if (!empty($_POST['type'])) 
		{
			$type = $_POST['type'];
			$sql_type = "SELECT id from type WHERE type ='$type'";
			$req_type = mysqli_query($db, $sql_type);
			$tab_type = mysqli_fetch_array($req_type);
			$id_type = $tab_type['id'];

			$query = "UPDATE bien SET id_type='$id_type' WHERE id='".$_GET['id']."';";
			$res = mysqli_query($db, $query);
			if ($res == false)
				echo "Erreur dans la Tarification id_type ";
			header("Refresh: 0;"); 
		}
			
		if (isset($_FILES['photo']))
		{
			$arrayImg = array('gif', 'jpg', 'jpeg', 'png', 'PNG');
			if($_FILES['photo']['size'] > 0)
			{
				$uploadFile = "photos/" . utf8_decode(basename($_FILES['photo']['name']));
				$file_extension = pathinfo($uploadFile, PATHINFO_EXTENSION);
				if (!in_array($file_extension, $arrayImg))
					$error = "Problème dans le format de l'image";
				if ($error != "" || !move_uploaded_file($_FILES['photo']['tmp_name'], $uploadFile))
					echo "Erreur lors de l'envoi du fichier";
			}
			else
			{
				$uploadFile = "";
			}		
			if ($uploadFile != "") 
			{
				$query = "UPDATE photo SET photo='$uploadFile' WHERE id_bien='".$_GET['id']."';";
				$res = mysqli_query($db, $query);
				if ($res == false)
					echo "Erreur dans la Tarification photo ";
				header("Refresh: 0;"); 
			}
			
		}
		
		if (!empty($_POST['adresse'])) 
		{
			$query = "UPDATE bien SET adresse='$adresse' WHERE id='".$_GET['id']."';";
			$res = mysqli_query($db, $query);
			if ($res == false)
				echo "Erreur dans la Tarification adresse ";
			header("Refresh: 0;"); 
		}

		if (!empty($_POST['description'])) 
		{
			$query = "UPDATE bien SET description='$description' WHERE id='".$_GET['id']."';";
			$res = mysqli_query($db, $query);
			if ($res == false)
				echo "Erreur dans la Tarification description ";
			header("Refresh: 0;"); 
		}

		if (!empty($_POST['surface'])) 
		{
			$query = "UPDATE bien SET surface='$surface' WHERE id='".$_GET['id']."';";
			$res = mysqli_query($db, $query);
			if ($res == false)
				echo "Erreur dans la Tarification surface ";
			header("Refresh: 0;"); 
		}

		if (!empty($_POST["nbsemaine"])) 
		{
			$query = "UPDATE reservation SET date_debut='$debut_location' AND date_fin='$fin_location' WHERE id='".$_GET['id']."';";
			$res = mysqli_query($db, $query);
			if ($res == false)
				echo "Erreur dans la Tarification date_debut date_fin ";
			header("Refresh: 0;"); 
		}
	
}



?>

<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="style.css">
	<meta charset="utf-8">
</head>
<body>
	<div id="conteneur">
		<div id="header"> <?php include_once("header.php"); ?><br/></div>
			<div id="contenu">
				<table border="1px" align="center">
				<?php 
                        while ( $data= mysqli_fetch_array($req))
                          { 

                                $sql_type = "SELECT type FROM type where id=".$data['id_type'];
                                $res_type = mysqli_query($db,$sql_type) or die('Erreur SQL !'.$sql_type.'<br>'.mysqli_error($db)); 
                                $data1 = mysqli_fetch_array($res_type);

                                $sql_tarif = "SELECT * FROM tarif where id=".$data['id_tarif'];
                                $res_tarif = mysqli_query($db,$sql_tarif) or die('Erreur SQL !'.$sql_tarif.'<br>'.mysqli_error($db));
                                $data2 = mysqli_fetch_array($res_tarif);

                                $sql_pays = "SELECT pays FROM pays where id=".$data['id_pays'];
                                $res_pays = mysqli_query($db,$sql_pays) or die('Erreur SQL !'.$sql_pays.'<br>'.mysqli_error($db)); 
                                $data3 = mysqli_fetch_array($res_pays);

                                $sql_user = "SELECT nom FROM user where id=".$data['id_user'];
                                $res_user = mysqli_query($db,$sql_user) or die('Erreur SQL !'.$sql_user.'<br>'.mysqli_error($db));
                                $data4 = mysqli_fetch_array($res_user);

                                $sql_photo = "SELECT photo FROM photo where id_bien=".$data['id'];
                                $res_photo = mysqli_query($db,$sql_photo) or die('Erreur SQL !'.$sql_photo.'<br>'.mysqli_error($db));
                                $data5 = mysqli_fetch_array($res_photo);

                                if ($data5['photo'] == "") {
                                    $sql_photo = "SELECT photo FROM photo where id_bien=0";
                                    $res_photo = mysqli_query($db,$sql_photo) or die('Erreur SQL !'.$sql_photo.'<br>'.mysqli_error($db));
                                    $data5 = mysqli_fetch_array($res_photo);
                                }

                                $sql_date = "SELECT * FROM reservation where id_bien=".$data['id'];
                                $res_date = mysqli_query($db,$sql_date) or die('Erreur SQL !'.$sql_date.'<br>'.mysqli_error($db));
                                $data6 = mysqli_fetch_array($res_date);

                                if(isset($_POST['supprimer'])){
                                    $id_bien = $data['id'];
                                    $query = "DELETE FROM bien WHERE id=".$id_bien;
                                    mysqli_query($db,"DELETE FROM bien WHERE id=".$id_bien);
                                    echo $query;
                                    //header('Location: index.php');
                                }

                                if ($data6['confirmation']==1) 
                                {
                                    $_SESSION['louer'] = "Ce bien est louée";
                                }
                                else
                                {
                                    $_SESSION['louer'] = "Ce bien n'est pas louée";
                                }

                                $sql_ville = "SELECT ville FROM ville where id=".$data['id_ville'];
                                $res_ville = mysqli_query($db,$sql_ville) or die('Erreur SQL !'.$sql_ville.'<br>'.mysqli_error($db)); 
                                $data7 = mysqli_fetch_array($res_ville);

                                echo "  <center>
                                            <tr>
                                                <td rowspan=7>
                                                    <img src=".$data5['photo'].">
                                                </td>
                                                <td>
                                                    Description : ".stripslashes($data['description'])."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Adresse : ".$data['adresse']."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Lieu : ".$data7['ville'].", ".$data3['pays']."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Surface : ".$data['surface']." m²
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Type : ".$data1['type']."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Tarif par jour : ".$data2['tarif_jour']." €
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Tarif par semaine : ".$data2['tarif_semaine']." €
                                                </td>
                                            </tr>
                                        </center>";
                          }
                ?>     
					<form method="post" ENCTYPE="multipart/form-data">
				<tr>
					<td><p>Tarifications semaine</p></td>
					<td>
						<input type="text" id="tarif" name="tarif_semaine" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Tarifications journée</p></td>
					<td>
						<input type="text" id="tarif" name="tarif_journee" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Pays du bien</p></td>
					<td>
						<select name="pays">
						    <?php
								while ($data_pays = mysqli_fetch_array($req_pays)) 
								{
									$pays_seletc = $data_pays['pays'];
									echo "<option value=\"$pays_seletc\" >".$pays_seletc."</option>";
								}
								?>
						</select>
					</td>
				</tr>
				<tr>
					<td><p>Ville du bien</p></td>
					<td>
						<select name="ville">
						    <?php
								while ($data_ville = mysqli_fetch_array($req_ville)) 
								{
									$ville_seletc = $data_ville['ville'];
									echo "<option value=\"$ville_seletc\" >".$ville_seletc."</option>";
								}
								?>
						</select>
					</td>
				</tr>
				<tr>
					<td><p>Adresse du bien</p></td>
					<td>
						<input type="textarea" id="adresse" name="adresse" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Code Postal</p></td>
					<td>
						<input type="textarea" id="postal" name="postal" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Pour combien de semaine</p></td>
					<td>
						<input type="textarea" id="nbsemaine" name="nbsemaine" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Début de location</p></td>
					<td>
						<select name="jour_debut">
							    <?php for ($jour = 1 ; $jour <= 31 ; $jour++){ ?>
							    <option value="<?php echo $jour ?>"><?php echo $jour; ?></option>
								<?php } ?>  
							</select>
							<select name="mois_debut">
		        				<?php for ($mois = 1 ; $mois <= 12 ; $mois++){ ?>
							    <option value="<?php echo $mois ?>"><?php echo $mois; ?></option>
								<?php } ?>  
							</select>
							<select name="annee_debut">
							    <?php for ($annee = 2014 ; $annee <= 2017 ; $annee++){ ?>
							    <option value="<?php echo $annee ?>"><?php echo $annee; ?></option>
								<?php } ?>  
							</select>
					</td>
				</tr>
					<td><p>Surface habitable</p></td>
					<td>
						<input type="text" id="surface" name="surface" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Type de bien</p></td>
					<td>
						<select name="type">
						    <?php
								while ($data_type = mysqli_fetch_array($req_type)) 
								{
									$type_seletc = $data_type['type'];
									echo "<option value=\"$type_seletc\" >".$type_seletc."</option>";
								}
								?>
						</select>
					</td>
				</tr>
				<tr>
					<td><p>Description du bien</p></td>
					<td>
						<textarea id="description" name="description" value=""></textarea>
					</td>
				</tr>
				<tr>
					<td>
						Photos du bien
					</td>
					<td>
						<input type="file" value="" id="photo" name="photo" title=""/>
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center"><input type="submit" id="envoyer" name="envoyer"  value="Envoyer"></td>
				</tr>
				
		</form>
	</table>
</div>
	<div id="footer"> <?php include_once("footer.html"); ?> </div>
</div>
</body>
</html>

